package com.games.game.mapper;

import com.games.game.bean.Game;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class User_GameMapper {
    static String JDBC_URL =  "jdbc:mysql://localhost/games?userSSL=true&useUnicode=true&characterEncoding=UTF8&serverTimezone=GMT";//?allowPublicKeyRetrieval=true&useSSL=false&characterEncoding=utf8";
    static String JDBC_USER = "root";
    static String JDBC_PASSWORD = "root";

    public List<String> get_all_game(String u_name){
        load();
        ArrayList<String> list = new ArrayList<>();
        try (Connection conn = (Connection) DriverManager.getConnection(JDBC_URL, JDBC_USER, JDBC_PASSWORD)) {
            try (PreparedStatement ps = conn.prepareStatement(
                    "SELECT * FROM user_games WHERE user_name = ?")) {
                ps.setObject(1, u_name);
                ResultSet rs = ps.executeQuery();
                while (rs.next()) {
                    String game_name = rs.getString(2);
                    list.add(game_name);
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }

    public boolean has_game(String u_name, String g_name) {
        load();
        try (Connection conn = (Connection) DriverManager.getConnection(JDBC_URL, JDBC_USER, JDBC_PASSWORD)) {
            try (PreparedStatement ps = conn.prepareStatement(
                    "SELECT * FROM user_games WHERE user_name = ? and game_name = ?")) {
                ps.setObject(1, u_name);
                ps.setObject(2, g_name);
                ResultSet rs = ps.executeQuery();
                while (rs.next()) {
                    return true;
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return false;
    }

    public void add_game(String u_name, String g_name) {
        load();
        try (Connection conn = (Connection) DriverManager.getConnection(JDBC_URL, JDBC_USER, JDBC_PASSWORD)) {
            try (PreparedStatement ps = conn.prepareStatement(
                    "INSERT INTO user_games VALUES (?, ?)")) {
                ps.setObject(1, u_name);
                ps.setObject(2, g_name);
                ps.executeUpdate();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public void del_game(String u_name, String g_name) {
        load();
        try (Connection conn = (Connection) DriverManager.getConnection(JDBC_URL, JDBC_USER, JDBC_PASSWORD)) {
            try (PreparedStatement ps = conn.prepareStatement(
                    "DELETE FROM user_games WHERE user_name=? and game_name=?")) {
                ps.setObject(1, u_name);
                ps.setObject(2, g_name);
                ps.executeUpdate();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public void load() {
        try {
            // The newInstance() call is a work around for some
            // broken Java implementations
            Class.forName("com.mysql.cj.jdbc.Driver");
//            System.out.println("加载成功");
        } catch (Exception ex) {
            System.out.println("加载失败");
            // handle the error
        }
    }

}
